const Router = require('koa-router')
const callCloudFn = require('../../../core/callCloudFn')
const callCloudDB = require('../../../core/callCloudDB.js')
const cloudStorage = require('../../../core/callCloudStorage.js')
const {
    periodicalAddValidator
} = require('../../validators/validator')
const {
    Auth
} = require('../../../middlewares/auth')

const router = new Router({
    prefix: '/v1/musicList'
})
// get post

router.get('/list', async (ctx, next) => { //列表
    // class
    const query = `
        db.collection('class').orderBy('index', 'asc').get()
    `
    const res = await callCloudDB(ctx, 'databasequery', query)
    let data = res.data.map(item => JSON.parse(item))

    // 图片
    let fileListImg = data.map(item => {
        return {
            fileid: item.img,
            max_age: 7200
        }
    })
    const dlResImg = await cloudStorage.download(ctx, fileListImg)
    // 
    let fileListMusic = data.map(item => {
        return {
            fileid: item.url,
            max_age: 7200
        }
    })
    const dlResMusic = await cloudStorage.download(ctx, fileListMusic)
    data.forEach(item => {
        dlResImg.file_list.forEach(op => {
            if (item.img == op.fileid) {
                item.img_url = op.download_url
            }
        })
    })
    data.forEach(item => {
        dlResMusic.file_list.forEach(op => {
            if (item.url == op.fileid) {
                item.music_url = op.download_url
            }
        })
    })
    ctx.body = {
        error_code: 0,
        msg: 'ok',
        data:{
            list:data,
            total:100
        }
    }
})

router.post('/onAddPlaylist', new Auth(1).m, async (ctx, next) => { //新增
    const v = await new periodicalAddValidator().validate(ctx)
    const params = {
        title: v.get('body.title'),
        type: v.get('body.type'),
        content: v.get('body.content'),
        index: v.get('body.index')
    }
    const query = `
        db.collection('class').add({
            data: {
                title: '${params.title}',
                type: ${params.type},
                index: ${params.index},
                content: '${params.content}'
            }
        })
    `
    const res = await callCloudDB(ctx, 'databaseadd', query)
    ctx.body = {
        code: 200,
        data: res
    }
})

router.post('/updateMusic', async (ctx, next) => {
    const fileid = await cloudStorage.upload(ctx)
    const params = ctx.request.body
    const query = `
        db.collection('class').doc('${params._id}').update({
            data: {
                url: '${fileid}',
            }
        })
    `
    const res = await callCloudDB(ctx, 'databaseupdate', query)
    ctx.body = {
        code: 200,
        data: res
    }
})

router.post('/updateImg', async (ctx, next) => {
    const fileid = await cloudStorage.upload(ctx)
    const params = ctx.request.body
    const query = `
        db.collection('class').doc('${params._id}').update({
            data: {
                img: '${fileid}',
            }
        })
    `
    const res = await callCloudDB(ctx, 'databaseupdate', query)
    ctx.body = {
        code: 200,
        data: res
    }
})

router.post('/updatePlaylist', async (ctx, next) => { // 更新
    const params = ctx.request.body
    const query = `
        db.collection('class').doc('${params._id}').update({
            data: {
                title: '${params.title}',
                index: '${params.index}',
                type: '${params.type}',
                content: '${params.content}'
            }
        })
    `
    const res = await callCloudDB(ctx, 'databaseupdate', query)
    ctx.body = {
        code: 200,
        data: res
    }
})

router.get('/del', async (ctx, next) => {
    // 删数据
    const params = ctx.request.query
    const query = `db.collection('class').doc('${params.id}').remove()`
    const res = await callCloudDB(ctx, 'databasedelete', query)
    //删图片
    const delStorageRes = await cloudStorage.delete(ctx, params.img)
    //删歌
    const musicRes = await cloudStorage.delete(ctx, params.url)
    ctx.body = {
        code: 200,
        data: res
    }
})

module.exports = router